using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Newtonsoft.Json;
using TMM.Common;

namespace TMM
{
    public class ServersManager: ServersManagerBase
    {
        WebClient webClient = null;

        public WebClient WebClient
        {
            get
            {
                if (webClient == null)
                    webClient = new WebClient();

                return webClient;
            }
        }

        public override T GetData<T>(string address)
        {
            try
            {
                var data = WebClient.DownloadString(address);
                if (string.IsNullOrWhiteSpace(data))
                    return default(T);

                var dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(data);

                return JsonConvert.DeserializeObject<T>(dic.ContainsKey("d") ? dic["d"].ToString() : data);
            }
            catch (Exception ex)
            {
                //TODO
            }

            return default(T);
        }
    }
}